import api from 'api/trainingProgram'

let state = {
  plan_list: [], // 训练计划模板列表
  temporary_detail_data: {}, // 临时存放的提交数据
  mode_sport_list: [], // 运动类型
  mode_style_list: [], // 训练方式列表
  edit_style_list: [] // 训练计划编辑时训练方式列表
}
const getters = {
  plan_list: state => state.plan_list,
  temporary_detail_data: state => state.temporary_detail_data,
  mode_sport_list: state => state.mode_sport_list,
  mode_style_list: state => state.mode_style_list,
  edit_style_list: state => state.edit_style_list
}

const actions = {
  plan_list ({ commit, state }, params) {
    state.plan_list = []
    return api.getPlanList(params).then((json) => {
      commit('PLAN_LIST', json)
    }).catch(() => {})
  },
  temporary_detail_data ({ commit, state }, data) {
    state.temporary_detail_data = {}
    return commit('TEMPORARY_DETAIL_DATA', data)
  },
  mode_sport_list ({commit, state}, params) {
    return api.searchSport(params).then((json) => {
      commit('MODE_SPORT_LIST', json)
    }).catch(() => {
      return false
    })
  },
  mode_style_list ({commit, state}, params) {
    if (params.type === 'edit') { // 编辑
      return api.getPlanTempList(params).then((json) => {
        commit('MODE_STYLE_LIST', json)
      })
    } else { // 新增
      return api.searchStyleList(params).then((json) => {
        commit('MODE_STYLE_LIST', json)
      })
    }
  }
}

const mutations = {
  PLAN_LIST (state, data) {
    state.plan_list = data.data
  },
  TEMPORARY_DETAIL_DATA (state, data) {
    state.temporary_detail_data = data
  },
  MODE_SPORT_LIST (state, data) {
    state.mode_sport_list = data.data
  },
  MODE_STYLE_LIST (state, data) {
    state.mode_style_list = data.data
  }
  // EDIT_STYLE_LIST (state, data) {
  //   state.edit_style_list = data.data
  // }
}

export default {
  state,
  getters,
  actions,
  mutations
}
